package com.itheima.Config;


import com.itheima.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;


@Slf4j
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    /**
     * 配置静态资源映射
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("配置静态资源映射...");
        //addResourceHandler设置暴露出来的请求路径，addResourceLocations设置资源存放的路径
        //eg: http://localhost:8080/backend/index.html, 就会去类路径下查找/backend/目录
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("配置消息转换器对象");
        //new消息转换器
        MappingJackson2HttpMessageConverter mjhmc = new MappingJackson2HttpMessageConverter();

        //添加自定义消息转换器
        mjhmc.setObjectMapper(new JacksonObjectMapper());

        //通过索引来设置自定义的消息转换器的优先级
        converters.add(0,mjhmc);
    }
}